//
//  HeadView.m
//  Project_B
//
//  Created by 王涛 on 15/12/16.
//  Copyright © 2015年 王涛. All rights reserved.
//

#import "HeadView.h"
#import "Weather.h"
@implementation HeadView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.weatherImageView = [[UIImageView alloc] initWithFrame:(CGRectMake(kWidth / 37.5, kHeight / 66.7, kWidth / 5.36, kHeight / 10.26))];
        self.weatherImageView.image = [UIImage imageNamed:@"air1.png"];
        [self addSubview:self.weatherImageView];
        
        self.temperatureLabel = [[UILabel alloc] initWithFrame:(CGRectMake(kWidth / 4.6875, kHeight / 23.82, kWidth / 3.75, kHeight / 25))];
        //        self.temperatureLabel.text = @"0° ~ 7°";
        self.temperatureLabel.font = [UIFont boldSystemFontOfSize:23];
        self.temperatureLabel.tintColor = [UIColor darkGrayColor];
        [self addSubview:self.temperatureLabel];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:(CGRectMake(kWidth / 1.39, kHeight / 33.35, kWidth / 4.41, kHeight / 9.53))];
        imageView.image = [UIImage imageNamed:@"air.png"];
        imageView.layer.borderWidth = 1;
        imageView.layer.cornerRadius = 5;
        imageView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        [self addSubview:imageView];
        
        self.airLabel = [[UILabel alloc] initWithFrame:(CGRectMake(kWidth / 1.39, kHeight / 14.82, kWidth / 4.41, kHeight / 16.675))];
        self.airLabel.textColor = [UIColor redColor];
        self.airLabel.textAlignment = NSTextAlignmentCenter;
        //        self.airLabel.text = @"60";
        self.airLabel.font = [UIFont boldSystemFontOfSize:25];
        self.airLabel.textColor = [UIColor redColor];
        [self addSubview:self.airLabel];
        
        self.xicheLabel = [[UILabel alloc] initWithFrame:(CGRectMake(kWidth / 18.75, kHeight / 8.89, kWidth / 2, kHeight / 33.35))];
        self.xicheLabel.font = [UIFont systemFontOfSize:14];
        self.xicheLabel.textColor = [UIColor lightGrayColor];
        //        self.xicheLabel.text = @"较适宜洗车｜多云";
        [self addSubview:self.xicheLabel];
        
        self.addressLabel = [[UILabel alloc] initWithFrame:(CGRectMake(kWidth / 18.75, kHeight / 6.06, kWidth / 4.6875, kHeight / 26.68))];
        self.addressLabel.text = @"上海";
        self.addressLabel.textColor = [UIColor lightGrayColor];
        self.addressLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.addressLabel];
        
        self.dateLabel = [[UILabel alloc] initWithFrame:(CGRectMake(kWidth / 1.42, kHeight / 6.06, kWidth / 3, kHeight / 26.68))];
        self.dateLabel.text = @"12/18 星期五";
        self.dateLabel.font = [UIFont systemFontOfSize:15];
        self.dateLabel.textColor = [UIColor lightGrayColor];
        [self addSubview:self.dateLabel];
    }
    return self;
}
- (void)setWeather:(Weather *)weather {
    if (_weather != weather) {
        _weather = weather;
        self.temperatureLabel.text = [[[NSString stringWithFormat:@"%ld",weather.lowtemperature ] stringByAppendingString:@"° ~ "] stringByAppendingString:[[NSString stringWithFormat:@"%ld",weather.hightemperature] stringByAppendingString:@"°"]];
        self.xicheLabel.text = [[weather.xiche stringByAppendingString:@"|"] stringByAppendingString:weather.weathertext];
        self.airLabel.text = [NSString stringWithFormat:@"%ld",weather.air];
        self.dateLabel.text = [[weather.date substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
        NSDate *date = [NSDate date];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *comps;
        comps = [calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)
                            fromDate:date];
        NSInteger weekday = [comps weekday]; // 星期几（注意，周日是“1”，周一是“2”。。。。）
        switch (weekday) {
            case 1:
                self.dateLabel.text = [[[weather.date substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期日"];
                break;
            case 2:
                self.dateLabel.text = [[[weather.date substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期一"];
                break;
            case 3:
                self.dateLabel.text = [[[weather.date substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期二"];
                break;
            case 4:
                self.dateLabel.text = [[[weather.date substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期三"];
                break;
            case 5:
                self.dateLabel.text = [[[weather.date substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期四"];
                break;
            case 6:
                self.dateLabel.text = [[[weather.date substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期五"];
                break;
            case 7:
                self.dateLabel.text = [[[weather.date substringFromIndex:5] stringByReplacingOccurrencesOfString:@"-" withString:@"/"] stringByAppendingString:@" 星期六"];
                break;
                
            default:
                break;
        }
    }
}




@end
